﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WpfApp4BlogReader.Commands;
using WpfApp4BlogReader.Models;
using WpfApp4BlogReader.Views;
using WpfControlLibrary4MVVMLib;

namespace WpfApp4BlogReader.ViewModels
{
    public class BlogVM : ViewModelBase<Blog, MainVM>
    {
        public BlogVM(Blog blog, MainVM parent)
           : base(blog, parent)
        {
            var notify = (INotifyCollectionChanged)blog.Posts;
            if (notify != null)
            {
                notify.CollectionChanged += delegate
                {
                    OnPropertyChanged("Posts");
                };
            }
        }

        ICommand _newPostCommand;
        public ICommand NewPostCommand
        {
            get
            {
                if (NewPostDialogService == null)
                    NewPostDialogService =
                       new StandardDialogService<NewPostWindow>();
                return _newPostCommand ?? (_newPostCommand =
                   new RelayCommand<BlogPost>(post => {
                       if (post == null)
                           post = new BlogPost();
                       var vm = new BlogPostVM { Model = post };
                       var dlg = NewPostDialogService;
                       dlg.ViewModel = vm;
                       if (dlg.ShowDialog() == true)
                       {
                           post.When = DateTime.Now;
                           var cmd = new ReversibleCommand(
                               Parent.UndoManager,
                               new NewBlogPostCommand(Model));
                           cmd.Execute(post);
                       }
                   }));
            }
        }

        //public ICommand NewPostCommand
        //{
        //    get
        //    {
        //        if (NewPostDialogService == null)
        //            NewPostDialogService = new
        //               StandardDialogService<NewPostWindow>();
        //        return _newPostCommand ?? (_newPostCommand =
        //           new RelayCommand<BlogPost>(post => {
        //               if (post == null)
        //                   post = new BlogPost();
        //               var vm = new BlogPostVM { Model = post };
        //               var dlg = NewPostDialogService;
        //               dlg.ViewModel = vm;
        //               if (dlg.ShowDialog() == true)
        //               {
        //                   post.When = DateTime.Now;
        //                   Model.Posts.Add(post);
        //                   OnPropertyChanged("Posts");
        //               }
        //           }));
        //    }
        //}

        public IDialogService NewPostDialogService { get; set; }


        public BloggerVM Blogger
        {
            get { return new BloggerVM { Model = Model.Blogger }; }
        }

        public IEnumerable<BlogPostVM> Posts
        {
            get
            {
                return Model.Posts.Select(post =>
                   new BlogPostVM { Model = post });
            }
        }
    }

    //public class BlogVM : ViewModelBase<Blog>
    //{

    //    ICommand _newPostCommand;
    //    public ICommand NewPostCommand
    //    {
    //        get
    //        {
    //            return _newPostCommand ?? (_newPostCommand =
    //               new RelayCommand(() =>
    //               {
    //                   var post = new BlogPostVM
    //                   {
    //                       Model = new BlogPost()
    //                   };
    //                   var dlg = new NewPostWindow
    //                   {
    //                       DataContext = post
    //                   };
    //                   if (dlg.ShowDialog() == true)
    //                   {
    //                       post.Model.When = DateTime.Now;
    //                       Model.Posts.Add(post.Model);
    //                       OnPropertyChanged("Posts");
    //                   }
    //               }));
    //        }
    //    }

    //    public BloggerVM Blogger
    //    {
    //        get { return new BloggerVM { Model = Model.Blogger }; }
    //    }

    //    public IEnumerable<BlogPostVM> Posts
    //    {
    //        get
    //        {
    //            return Model.Posts.Select(post =>
    //               new BlogPostVM { Model = post });
    //        }
    //    }
    //}
}
